import { useRef } from 'react'
import { Modal, message } from 'antd'

import AuthForm from './AuthForm'

import type { RecordItem } from '../type'
import request from "@/utils/request";

interface Props {
  open?: boolean
  selRecord?: RecordItem
  onCancel: () => void
  onSearch?: (params: any) => void
}

const AuthModal: React.FC<Props> = ({ open, onCancel, selRecord, onSearch }) => {
  const authFromRef = useRef<any>(null)

  const handleCancel = () => {
    onCancel()
  }

  const handleOk = async () => {
    try {
      const values = await authFromRef.current.validateFields()
      request('tenantManageResourceEdit', { data: { ...values, tenantId: selRecord?.tenantId } }).then(
        res => {
          if (res.code === 0) {
            message.success('授权成功')
            onSearch?.({})
            onCancel()
          }
        }
      )

    } catch (error) {
      console.log(error)
    }
  }

  return (
    <Modal
      title="租户授权"
      width={640}
      maskClosable={false}
      open={open}
      bodyStyle={{ maxHeight: 550, overflowY: 'auto' }}
      onCancel={handleCancel}
      onOk={handleOk}
    >
      <AuthForm ref={authFromRef} selRecord={selRecord}></AuthForm>
    </Modal>
  )
}

export default AuthModal
